struct Solution;

impl Solution {
    pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
        let mut init: i32 = card_points.iter().skip(card_points.len() - k as usize).sum();
        let mut res = init;
        for (sub, add) in card_points.iter().skip(card_points.len() - k as usize).
            zip(card_points.iter().take(k as usize)) {
            init -= sub;
            init += add;
            if init > res {
                res = init;
            }
        }

        res
    }
}

#[cfg(test)]
mod test {
    use crate::Solution;

    #[test]
    fn test_01() {
        let res = Solution::max_score(vec![1, 2, 3, 4, 5, 6, 1], 3);
        assert_eq!(res, 12);
    }

    #[test]
    fn test_02() {
        let res = Solution::max_score(vec![2, 2, 2], 2);
        assert_eq!(res, 4);
    }

    #[test]
    fn test_03() {
        let res = Solution::max_score(vec![9, 7, 7, 9, 7, 7, 9], 7);
        assert_eq!(res, 55);
    }

    #[test]
    fn test_04() {
        let res = Solution::max_score(vec![1, 1000, 1], 1);
        assert_eq!(res, 1);
    }

    #[test]
    fn test_05() {
        let res = Solution::max_score(vec![1, 79, 80, 1, 1, 1, 200, 1], 3);
        assert_eq!(res, 202);
    }

    #[test]
    fn test_06() {
        let res = Solution::max_score(vec![100, 40, 17, 9, 73, 75], 3);
        assert_eq!(res, 248);
    }
}

fn main() {
    let res = Solution::max_score(vec![100, 40, 17, 9, 73, 75], 3);
    assert_eq!(res, 248);
}
